/**
 * Company : Thai Informatic System Co., Ltd.
 * Created By : Dr.Tree
 * Created On : Feb 15, 2012  12:29:45 PM 
 * e-Mail : chatree@tis.co.th
 * Mobile Phone : (+66) 087-921-5115
 * Package Name : apps.promis.in.common
 * File Name : CostArticleAction.java
 * TODO
 */
package apps.promis.in.common;

import java.util.ArrayList;
import java.util.List;

import javax.management.DescriptorKey;

import org.apache.log4j.Logger;

import jpa.entitys.promis.common.CostArticle;
import jpa.entitys.promis.common.CostArticlePK;
import jpa.entitys.promis.common.CostProject;
import jpa.entitys.promis.common.CostProjectArticle;
import jpa.entitys.promis.common.CostProjectArticlePK;
import jpa.entitys.promis.common.CostProjectPK;
import jpa.entitys.synchro.staging.MmMatmasInb;
import jpa.service.promis.common.CostArticleServices;
import jpa.service.promis.common.ProjectServices;
import jpa.service.synchro.staging.MmMatmasInbServices;
import promis.utility.ACTION_HANDLER;
import promis.utility.CommonService;
import promis.utility.Utilitys;
import apps.sessionFactory;

/**
 * 
 * @author Chatree Siangklom of Administrator
 * Date =Nov 13, 2013 : 2:30:13 PM 
 * CreateBy=Chatree Siangklom
 * 
 * CostArticleAction
 * TODO
 */
public class CostArticleAction {
	
	private static final Logger log = Logger.getLogger(CostArticleAction.class);

	/**
	 * 
	 */
	public CostArticleAction() {
		// TODO Auto-generated constructor stub
	}
	
	/**
	 * 
	 * Date =Nov 13, 2013 : 2:30:17 PM 
	 * CreateBy=Chatree Siangklom
	 * CostArticle
	 * TODO
	 * @param articleGroup
	 * @param articleSubGroup
	 * @param articleCode
	 * @return
	 * @throws Exception
	 */
	@Deprecated
	public CostArticle FindByKey(String articleGroup, String articleSubGroup, String articleCode) throws Exception {
		
		try{
			
			CostArticleServices costArticleServ = new CostArticleServices(sessionFactory.CreateEmPromis());
			CostArticlePK costArticlePK = new CostArticlePK();
			costArticlePK.setArticleCode(articleCode);
			
			//costArticlePK.setArticleGroup(articleGroup);
			//costArticlePK.setArticleSubGroup(articleSubGroup);
			
			return costArticleServ.findById(articleCode);	
			
		}catch(Exception e){
			System.out.println(e.getMessage());
			log.error(e.getMessage());
			throw e;
		}		
	}
	
	/**
	 * 
	 * Date =Nov 13, 2013 : 2:30:20 PM 
	 * CreateBy=Chatree Siangklom
	 * CostArticle
	 * TODO
	 * @param articleCode
	 * @return
	 * @throws Exception
	 */
	public CostArticle FindByKey(String articleCode) throws Exception {		
		try{

			CostArticleServices costArticleServ = new CostArticleServices(sessionFactory.CreateEmPromis());			
			return costArticleServ.findById(articleCode);	
			
		}catch(Exception e){
			System.out.println(e.getMessage());
			log.error(e.getMessage());
			throw e;
		}		
	}
	
	/**
	 * 
	 * Date =Nov 13, 2013 : 2:30:23 PM 
	 * CreateBy=Chatree Siangklom
	 * boolean
	 * TODO
	 * @param articleGroup
	 * @param articleSubGroup
	 * @param articleCode
	 * @return
	 * @throws Exception
	 */
	public boolean ExistingChecked(String articleGroup, String articleSubGroup, String articleCode) throws Exception {
		try{
			
			return FindByKey(articleGroup, articleSubGroup, articleCode) != null ? true : false;
			
		}catch(Exception e){
			System.out.println(e.getMessage());
			log.error(e.getMessage());
			throw e;
		}		
	}
	
	/**
	 * 
	 * Date =Nov 13, 2013 : 2:30:25 PM 
	 * CreateBy=Chatree Siangklom
	 * CostArticle
	 * TODO
	 * @param mmMatmasInb
	 * @return
	 * @throws Exception
	 */
	public CostArticle SaveOrUpdateCostArticle(MmMatmasInb mmMatmasInb) throws Exception {
		
		try{
			
			CostArticleServices costArticleServ = new CostArticleServices(sessionFactory.CreateEmPromis());
			CostArticle model = FindByKey(mmMatmasInb.getArticleCode());		
			
			if(model == null){
				model = new CostArticle();				
				
				//Set Id								
				model.setArticleCode(mmMatmasInb.getArticleCode());				
				model.setArticleType(mmMatmasInb.getArtiTypeCode());				
				model.setArticleGroup(mmMatmasInb.getArtiGrpCode());
				model.setArticleSubGroup(mmMatmasInb.getArtiSubGrpCode());				
				model.setDescription(mmMatmasInb.getDescription());
				model.setDescriptionTh(mmMatmasInb.getDescriptionTh());
				model.setUm(mmMatmasInb.getUm());
				model.setStatus(mmMatmasInb.getStatus());
				model.setArticleType(mmMatmasInb.getArtiTypeCode());
				model.setItemType("NN");
				model.setDepreciationType("L");
				model.setValuationMethod("FIFO");
				model.setPrFlag(mmMatmasInb.getPrFlag());
				
				model.setProductType(mmMatmasInb.getProductType());
				model.setProductCode(mmMatmasInb.getProductCode());
				model.setModelCode(mmMatmasInb.getModelCode());
				model.setBrandName(mmMatmasInb.getBrandName());
				model.setMaterialType(mmMatmasInb.getMaterialType());
				model.setProductColor(mmMatmasInb.getProductColor());
				model.setProductSize(mmMatmasInb.getProductSize());
				model.setInstalllocation(mmMatmasInb.getInstalllocation());
				model.setProductdetail(mmMatmasInb.getProductdetail());
				model.setManufactures(mmMatmasInb.getManufactures());
				model.setPicturepath(mmMatmasInb.getPicturepath());
				
				model.setSyncIssync("N");
				model.setCreatedOn(Utilitys.ConvertStringToDateTime(mmMatmasInb.getCreatedOn()));
				model.setCreatedBy(mmMatmasInb.getCreatedBy());
				
				return costArticleServ.InsertedOrUpdated(model, ACTION_HANDLER.INSERTED);
				
			}else{
				
				model.setArticleGroup(mmMatmasInb.getArtiGrpCode());
				model.setArticleSubGroup(mmMatmasInb.getArtiSubGrpCode());
				
				model.setDescription(mmMatmasInb.getDescription());
				model.setDescriptionTh(mmMatmasInb.getDescriptionTh());
				model.setUm(mmMatmasInb.getUm());
				//model.setStatus(mmMatmasInb.getStatus());
				model.setArticleType(mmMatmasInb.getArtiTypeCode());
				model.setItemType("NN");
				model.setDepreciationType("L");
				model.setValuationMethod("FIFO");
				model.setPrFlag(mmMatmasInb.getPrFlag());	
				
				model.setProductType(mmMatmasInb.getProductType());
				model.setProductCode(mmMatmasInb.getProductCode());
				model.setModelCode(mmMatmasInb.getModelCode());
				model.setBrandName(mmMatmasInb.getBrandName());
				model.setMaterialType(mmMatmasInb.getMaterialType());
				model.setProductColor(mmMatmasInb.getProductColor());
				model.setProductSize(mmMatmasInb.getProductSize());
				model.setInstalllocation(mmMatmasInb.getInstalllocation());
				model.setProductdetail(mmMatmasInb.getProductdetail());
				model.setManufactures(mmMatmasInb.getManufactures());
				model.setPicturepath(mmMatmasInb.getPicturepath());
				
				model.setSyncIssync("N");
				
				if(CommonService.IsNull(mmMatmasInb.getUpdatedOn()))
					model.setLastUpdOn(Utilitys.ConvertStringToDateTime(mmMatmasInb.getCreatedOn()));				
				else
					model.setLastUpdOn(Utilitys.ConvertStringToDateTime(mmMatmasInb.getUpdatedOn()));
				
				model.setLastUpdBy(CommonService.IsNullRNull(mmMatmasInb.getCreatedBy()));
				
				return costArticleServ.InsertedOrUpdated(model, ACTION_HANDLER.UPDATED);
			}
			
		}catch(Exception e){
			System.out.println(e.getMessage());
			log.error(e.getMessage());
			throw e;
		}
		
	}

	/**
	 * 
	 * Date =Nov 13, 2013 : 2:30:32 PM 
	 * CreateBy=Chatree Siangklom
	 * void
	 * TODO
	 * @param lst
	 * @throws Exception
	 */
	public void Processing(List<MmMatmasInb> lst) throws Exception {
		try{
			
			MmMatmasInbServices mmMatmasInbServ = new MmMatmasInbServices(sessionFactory.CreateEmSynchro());
				
			if(!CommonService.IsNull(lst))
			{
				for(MmMatmasInb mmMatmasInb : lst)
				{	
					try{
						
						System.out.println("MmMatmasInb.getInstanceNo : " + mmMatmasInb.getInstanceNo());
						System.out.println("PromisStagingId : " + mmMatmasInb.getPromisStagingId() + " //SAP ExtRefKey : " + mmMatmasInb.getExtRefKey() + " // ArticleCode : " + mmMatmasInb.getArticleCode() + " // ArtiGrpCode : " + mmMatmasInb.getArtiGrpCode() + " // ArtiSubGrpCode : " + mmMatmasInb.getArtiSubGrpCode());
						System.out.println("Article Master ExistingChecked : " + ExistingChecked(mmMatmasInb.getArtiGrpCode() , mmMatmasInb.getArtiSubGrpCode(), mmMatmasInb.getArticleCode()));						
						
						//Group 
						CostArticleGroupAction costArtiGroupSrv = new CostArticleGroupAction();
						costArtiGroupSrv.SaveOrUpdateArticleGroup(mmMatmasInb);
						
						//Sub Group
						CostArticleSubGroupAction costArticleSubGroup = new CostArticleSubGroupAction();
						costArticleSubGroup.SaveOrUpdateArticleSubGroup(mmMatmasInb);
						
						//Article Master
						CostArticleAction costArticleAction = new CostArticleAction();
						costArticleAction.SaveOrUpdateCostArticle(mmMatmasInb);
						
						//Project Article
						CostProjectArticleAction  costProjectArticleAction = new CostProjectArticleAction();
						costProjectArticleAction.SaveOrUpdateCostArticle(mmMatmasInb);
						
						mmMatmasInb.setIssync("Y");
						mmMatmasInb.setMsgCode("S");
						mmMatmasInb.setMsgDesc("Success");
						mmMatmasInb.setMsgActResult(null);
						mmMatmasInbServ.Updated(mmMatmasInb);
						
					}catch(Exception e)
					{
						mmMatmasInb.setIssync("N");
						mmMatmasInb.setMsgCode("E");
						mmMatmasInb.setMsgDesc(CommonService.CutString(e.getMessage(), 3500));
						mmMatmasInb.setMsgActResult(CommonService.CutString(e.getMessage(), 3500));
						mmMatmasInbServ.Updated(mmMatmasInb);
						
						System.out.println(e.getMessage());
						log.error(e.getMessage());					
						throw e;
					}
					
				}
			}
			
		}catch(Exception e)
		{
			System.out.println(e.getMessage());
			log.error(e.getMessage());
			throw e;
		}		
	}
	
	/**
	 * 
	 * Date =Nov 13, 2013 : 2:30:47 PM 
	 * CreateBy=Chatree Siangklom
	 * void
	 * TODO
	 */
	@Deprecated
	private void testedd() {
		
		
		List<MmMatmasInb> lst = new ArrayList<MmMatmasInb>();
		try{
			MmMatmasInbServices mmMatmasInbServ = new MmMatmasInbServices(sessionFactory.CreateEmSynchro());
			//lst =  mmMatmasInbServ.findAllIsSync("N");
			
			//lst = new ArrayList<MmMatmasInb>();
			//lst.add(mmMatmasInbServ.findById("10000000000000039068"));
			
			//if(!CommonService.IsNull(lst))
			{
				//for(MmMatmasInb mmMatmasInb : lst)
				MmMatmasInb mmMatmasInb = mmMatmasInbServ.findById("10000000000000039067");
				{	
					if(mmMatmasInb != null){
						try{
							log.info(" mmMatmasInb.getInstanceNo : " + mmMatmasInb.getInstanceNo());
							log.info(" mmMatmasInb.getArticleCode : " + mmMatmasInb.getArticleCode());
							//log.info(" ExistingChecked : " + ExistingChecked(mmMatmasInb.getArtiGrpCode() , mmMatmasInb.getArtiSubGrpCode(), mmMatmasInb.getArticleCode()));
							log.info("PromisStagingId : " + mmMatmasInb.getPromisStagingId() + " getArticleCode : " + mmMatmasInb.getArticleCode() + " getArtiGrpCode : " + mmMatmasInb.getArtiGrpCode() + " getArtiSubGrpCode : " + mmMatmasInb.getArtiSubGrpCode());
							
							//Group 
							CostArticleGroupAction costArtiGroupSrv = new CostArticleGroupAction();
							costArtiGroupSrv.SaveOrUpdateArticleGroup(mmMatmasInb);
							
							//Sub Group
							CostArticleSubGroupAction costArticleSubGroup = new CostArticleSubGroupAction();
							costArticleSubGroup.SaveOrUpdateArticleSubGroup(mmMatmasInb);
							
							//Article Master
							CostArticleAction costArticleAction = new CostArticleAction();
							costArticleAction.SaveOrUpdateCostArticle(mmMatmasInb);
							
							//Project Article
							CostProjectArticleAction  costProjectArticleAction = new CostProjectArticleAction();
							costProjectArticleAction.SaveOrUpdateCostArticle(mmMatmasInb);
							
							mmMatmasInb.setIssync("Y");
							mmMatmasInb.setMsgCode("S");
							mmMatmasInb.setMsgDesc("Success");
							mmMatmasInb.setMsgActResult(null);
							mmMatmasInbServ.Updated(mmMatmasInb);
							
						}catch(Exception e)
						{
							log.error(e.getMessage());
							System.err.println(e.getMessage());
							mmMatmasInb.setIssync("N");
							mmMatmasInb.setMsgCode("E");
							mmMatmasInb.setMsgDesc("Error");
							mmMatmasInb.setMsgActResult(e.getMessage());
							mmMatmasInbServ.Updated(mmMatmasInb);
						}
					}
				}
			}
		}catch(Exception e)
		{
			System.out.println(e.getMessage());
			log.error(e.getMessage());
		}
		
	}

	/**
	 * Date =Nov 13, 2013 : 2:30:55 PM 
	 * CreateBy=Chatree Siangklom
	 * void
	 * TODO
	 * @param args
	 */
	public static void main(String [] args)
	{
		try{
			CostArticleAction obj = new CostArticleAction();			
			obj.testedd();
		}catch(Exception e)
		{
			log.error(e.getMessage());
			System.err.println(e.getMessage());
		}
	}
	
}
